package io.modelcontextprotocol.autoconfigure.utils;

import cn.pinming.pmsuite.utils.DateUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.BeanUtils;
import org.springframework.core.KotlinDetector;
import org.springframework.util.ClassUtils;

public abstract class JacksonUtils {
    public JacksonUtils() {
    }

    public static List<Module> instantiateAvailableModules() {
        List<Module> modules = new ArrayList();

        Class kotlinModuleClass;
        Module kotlinModule;
        try {
            kotlinModuleClass = ClassUtils.forName("com.fasterxml.jackson.datatype.jdk8.Jdk8Module", (ClassLoader)null);
            kotlinModule = (Module)BeanUtils.instantiateClass(kotlinModuleClass);
            modules.add(kotlinModule);
        } catch (ClassNotFoundException var6) {
        }

        try {
            kotlinModuleClass = ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", (ClassLoader)null);
            kotlinModule = (Module)BeanUtils.instantiateClass(kotlinModuleClass);
            modules.add(kotlinModule);
        } catch (ClassNotFoundException var5) {
        }

        try {
            kotlinModuleClass = ClassUtils.forName("com.fasterxml.jackson.module.paramnames.ParameterNamesModule", (ClassLoader)null);
            kotlinModule = (Module)BeanUtils.instantiateClass(kotlinModuleClass);
            modules.add(kotlinModule);
        } catch (ClassNotFoundException var4) {
        }

        if (KotlinDetector.isKotlinPresent()) {
            try {
                kotlinModuleClass = ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", (ClassLoader)null);
                kotlinModule = (Module)BeanUtils.instantiateClass(kotlinModuleClass);
                modules.add(kotlinModule);
            } catch (ClassNotFoundException var3) {
            }
        }

        // 注册 JavaTimeModule 以支持 Java 8 的日期时间类型
        JavaTimeModule javaTimeModule = new JavaTimeModule();

        // 自定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
        javaTimeModule.addSerializer(Date.class, new DateJsonSerializer( ));

        modules.add(javaTimeModule);
        return modules;
    }

    public static class DateJsonSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(DateUtils.THREAD_LOCAL_SDF3.get().format(date));
        }
    }
}